package com.shenbianyun.bosskgpay.service.sign;

import com.shenbianyun.bosskgpay.utils.IDCard;
import com.shenbianyun.bosskgpay.utils.RandInfo;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

/**
 * @author ：张一凡
 * @date ：2023年02月21日 17:37:48
 * @description：例子
 */
public class aaaa extends AbstractJavaSamplerClient {


    private String Name = "";
    private String PayAcc = "";
    private String Idcard = "";
    private String Mobile = "";

    //设置可用参数及的默认值；
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("Name", new RandInfo().randFamilyName());//设置参数，签约者姓名
        params.addArgument("PayAcc", "6214830114752289");//设置参数，签约者收款卡号
        params.addArgument("Idcard", new IDCard().getIDCard());//设置参数，签约者身份证号
        params.addArgument("Mobile", "aaaa请求通过，其他请求失败");//设置参数，签约者手机号
        return params;
    }




    //测试执行的循环体，根据线程数和循环次数的不同可执行多次，类似于LoadRunner中的Action方法
    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        //不要把他写成成员变量，因为当线程数设置大于1时会抛异常，只能写为局部变量
        SampleResult sampleResult = new SampleResult();

        try {
            sampleResult.sampleStart();//定义一个事务，表示这是事务的起始点，类似于LoadRunner的lr.start_transaction
            Name = javaSamplerContext.getParameter("Name");
            PayAcc = javaSamplerContext.getParameter("PayAcc");
            Idcard = javaSamplerContext.getParameter("Idcard");
            Mobile = javaSamplerContext.getParameter("Mobile");

            if (Mobile.equals("aaaa")) {
                //sampleResult的set会显示在结果树的响应数据里。system输出会输出在Jmeter启动的命令窗口中
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true + " " + Name + PayAcc + " " + Idcard + " " + Mobile, SampleResult.TEXT);
                System.out.println("结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseCode("201");
                sampleResult.setResponseMessage("FAIL");
                sampleResult.setResponseData(false + " " + Name + PayAcc + " " + Idcard + " " + Mobile, SampleResult.TEXT);
                System.out.println("结果:" + false + " " + Name + " " + PayAcc + " " + Idcard + " " + Mobile);
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(Name + PayAcc + " " + Idcard + " " + Mobile, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();//定义一个事务，表示这是事务的结束点，类似于LoadRunner的lr.end_transaction
        }

        return sampleResult;
    }


    //结束方法，实际运行时每个线程仅执行一次，在测试方法运行结束后执行，类似于LoadRunner中的end方法
    public void teardownTest(JavaSamplerContext arg0) {
    }


    public static void main(String[] args) {

        //调试 jmeter的java请求代码
        Arguments params = new Arguments();
        params.addArgument("Name", "1");//设置参数，并赋予默认值
        params.addArgument("PayAcc", "2");//设置参数，并赋予默认值
        params.addArgument("Idcard", "3");//设置参数，并赋予默认值
        params.addArgument("Mobile", "aaaa");//设置参数，并赋予默认值
        JavaSamplerContext arg0 = new JavaSamplerContext(params);
        aaaa test = new aaaa();
        test.setupTest(arg0);
        test.runTest(arg0);
        test.teardownTest(arg0);
    }

}
